﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using ChessProject_new;

namespace ChessProject_new
{
    public partial class TwoPlayer : Form
    {
        // Activate the Controller Object
        public Control c = new Control();

        // The Graphics generator for de-flickering
        public Graphics OffScreen;

        // The Graphics generator for the main screen
        public Graphics GFX;

        // The form code
        public TwoPlayer()
        {
            PiecesStart(c);
            InitializeComponent();
            GFX = this.CreateGraphics();
            this.SetStyle(ControlStyles.Opaque, true);
        }

        // The continuous loop through the game
        protected override void OnPaint(PaintEventArgs e)
        {
            this.Invalidate();

            if (c.piececlickedloop.IsLooping == true)
            {
                System.Drawing.Point mouse = PointToClient(new System.Drawing.Point(Cursor.Position.X - 38, Cursor.Position.Y - 38));
                if (mouse.X > -38 && mouse.X < 570)
                {
                    c.piececlickedloop.piece.x = mouse.X;
                }
                if ( mouse.Y > -38 &&mouse.Y < 570)
                {
                    c.piececlickedloop.piece.y = mouse.Y;
                }
            }

            Bitmap HBit = new Bitmap(849, 608);
            Graphics gBmp = Graphics.FromImage(HBit);
            gBmp.DrawImage(Bitmap.FromFile("Images/Board.png"), new Rectangle(0, 0, 849, 608));
            gBmp.DrawImage(c.pieceset.wp1.Sprite, new Rectangle(c.pieceset.wp1.x, c.pieceset.wp1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp2.Sprite, new Rectangle(c.pieceset.wp2.x, c.pieceset.wp2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp3.Sprite, new Rectangle(c.pieceset.wp3.x, c.pieceset.wp3.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp4.Sprite, new Rectangle(c.pieceset.wp4.x, c.pieceset.wp4.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp5.Sprite, new Rectangle(c.pieceset.wp5.x, c.pieceset.wp5.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp6.Sprite, new Rectangle(c.pieceset.wp6.x, c.pieceset.wp6.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp7.Sprite, new Rectangle(c.pieceset.wp7.x, c.pieceset.wp7.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wp8.Sprite, new Rectangle(c.pieceset.wp8.x, c.pieceset.wp8.y, 76, 76));

            gBmp.DrawImage(c.pieceset.bp1.Sprite, new Rectangle(c.pieceset.bp1.x, c.pieceset.bp1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp2.Sprite, new Rectangle(c.pieceset.bp2.x, c.pieceset.bp2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp3.Sprite, new Rectangle(c.pieceset.bp3.x, c.pieceset.bp3.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp4.Sprite, new Rectangle(c.pieceset.bp4.x, c.pieceset.bp4.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp5.Sprite, new Rectangle(c.pieceset.bp5.x, c.pieceset.bp5.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp6.Sprite, new Rectangle(c.pieceset.bp6.x, c.pieceset.bp6.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp7.Sprite, new Rectangle(c.pieceset.bp7.x, c.pieceset.bp7.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bp8.Sprite, new Rectangle(c.pieceset.bp8.x, c.pieceset.bp8.y, 76, 76));

            gBmp.DrawImage(c.pieceset.wk.Sprite, new Rectangle(c.pieceset.wk.x, c.pieceset.wk.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wq.Sprite, new Rectangle(c.pieceset.wq.x, c.pieceset.wq.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wb1.Sprite, new Rectangle(c.pieceset.wb1.x, c.pieceset.wb1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wb2.Sprite, new Rectangle(c.pieceset.wb2.x, c.pieceset.wb2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wr1.Sprite, new Rectangle(c.pieceset.wr1.x, c.pieceset.wr1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wr2.Sprite, new Rectangle(c.pieceset.wr2.x, c.pieceset.wr2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wn1.Sprite, new Rectangle(c.pieceset.wn1.x, c.pieceset.wn1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.wn2.Sprite, new Rectangle(c.pieceset.wn2.x, c.pieceset.wn2.y, 76, 76));

            gBmp.DrawImage(c.pieceset.bk.Sprite, new Rectangle(c.pieceset.bk.x, c.pieceset.bk.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bq.Sprite, new Rectangle(c.pieceset.bq.x, c.pieceset.bq.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bb1.Sprite, new Rectangle(c.pieceset.bb1.x, c.pieceset.bb1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bb2.Sprite, new Rectangle(c.pieceset.bb2.x, c.pieceset.bb2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.br1.Sprite, new Rectangle(c.pieceset.br1.x, c.pieceset.br1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.br2.Sprite, new Rectangle(c.pieceset.br2.x, c.pieceset.br2.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bn1.Sprite, new Rectangle(c.pieceset.bn1.x, c.pieceset.bn1.y, 76, 76));
            gBmp.DrawImage(c.pieceset.bn2.Sprite, new Rectangle(c.pieceset.bn2.x, c.pieceset.bn2.y, 76, 76));
            GFX.DrawImageUnscaled(HBit, 0, 0, 849, 608);
        }

        // Action for when the mouse is pressed
        protected override void  OnMouseDown(MouseEventArgs e)
        {
            int squarex = (PointToClient(new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y)).X / 76) * 76;
            int squarey = (PointToClient(new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y)).Y / 76) * 76;
            if (squarex >= 0 && squarex < 608 && squarey >= 0 && squarey < 608)
            {
                for (var i = 0; i < 8; i++)
                {
                    if (c.pieceset.WhiteTurn == true)
                    {
                        if (c.pa.WhitePawns[i].x == squarex && c.pa.WhitePawns[i].y == squarey)
                        {
                            c.pa.WhitePawns[i].px = squarex;
                            c.pa.WhitePawns[i].py = squarey;
                            c.piececlickedloop.piece = c.pa.WhitePawns[i];
                            c.piececlickedloop.IsLooping = true;
                        }
                        else if (c.pa.WhitePieces[i].x == squarex && c.pa.WhitePieces[i].y == squarey)
                        {
                            c.pa.WhitePieces[i].px = squarex;
                            c.pa.WhitePieces[i].py = squarey;
                            c.piececlickedloop.piece = c.pa.WhitePieces[i];
                            c.piececlickedloop.IsLooping = true;
                        }
                    }
                    else
                    {
                        if (c.pa.BlackPawns[i].x == squarex && c.pa.BlackPawns[i].y == squarey)
                        {
                            c.pa.BlackPawns[i].px = squarex;
                            c.pa.BlackPawns[i].py = squarey;
                            c.piececlickedloop.piece = c.pa.BlackPawns[i];
                            c.piececlickedloop.IsLooping = true;
                        }
                        else if (c.pa.BlackPieces[i].x == squarex && c.pa.BlackPieces[i].y == squarey)
                        {
                            c.pa.BlackPieces[i].px = squarex;
                            c.pa.BlackPieces[i].py = squarey;
                            c.piececlickedloop.piece = c.pa.BlackPieces[i];
                            c.piececlickedloop.IsLooping = true;
                        }
                    }
                }
            }
        }

        // Action for when the mouse is released
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (c.piececlickedloop.piece != null)
            {
                c.piececlickedloop.IsLooping = false;
                c.piececlickedloop.piece.x = ((c.piececlickedloop.piece.x + 38) / 76 * 76);
                c.piececlickedloop.piece.y = ((c.piececlickedloop.piece.y + 38) / 76 * 76);

                if (c.piececlickedloop.piece.px != c.piececlickedloop.piece.x || c.piececlickedloop.piece.py != c.piececlickedloop.piece.y)
                {
                    if (c.pieceset.WhiteTurn == true)
                    {
                        c.pieceset.WhiteTurn = false;
                    }
                    else
                    {
                        c.pieceset.WhiteTurn = true;
                    }
                }

                for (var i = 0; i < 8; i++)
                {
                    if (c.piececlickedloop.piece.x == c.pa.WhitePawns[i].x && c.piececlickedloop.piece.y == c.pa.WhitePawns[i].y && c.piececlickedloop.piece != c.pa.WhitePawns[i])
                    {
                        if (c.piececlickedloop.piece.white != c.pa.WhitePawns[i].white)
                        {
                            c.pa.WhitePawns[i].x = c.pa.WhitePawns[i].dx;
                            c.pa.WhitePawns[i].y = c.pa.WhitePawns[i].dy;

                        }
                        else
                        {
                            c.piececlickedloop.piece.x = c.piececlickedloop.piece.px;
                            c.piececlickedloop.piece.y = c.piececlickedloop.piece.py;
                            if (c.pieceset.WhiteTurn == true)
                            {
                                c.pieceset.WhiteTurn = false;
                            }
                            else
                            {
                                c.pieceset.WhiteTurn = true;
                            }
                        }
                    }
                    if (c.piececlickedloop.piece.x == c.pa.BlackPawns[i].x && c.piececlickedloop.piece.y == c.pa.BlackPawns[i].y && c.piececlickedloop.piece != c.pa.BlackPawns[i])
                    {
                        if (c.piececlickedloop.piece.white != c.pa.BlackPawns[i].white)
                        {
                            c.pa.BlackPawns[i].x = c.pa.BlackPawns[i].dx;
                            c.pa.BlackPawns[i].y = c.pa.BlackPawns[i].dy;
                        }
                        else
                        {
                            c.piececlickedloop.piece.x = c.piececlickedloop.piece.px;
                            c.piececlickedloop.piece.y = c.piececlickedloop.piece.py;
                            if (c.pieceset.WhiteTurn == true)
                            {
                                c.pieceset.WhiteTurn = false;
                            }
                            else
                            {
                                c.pieceset.WhiteTurn = true;
                            }
                        }
                    }
                    if (c.piececlickedloop.piece.x == c.pa.WhitePieces[i].x && c.piececlickedloop.piece.y == c.pa.WhitePieces[i].y && c.piececlickedloop.piece != c.pa.WhitePieces[i])
                    {
                        if (c.piececlickedloop.piece.white != c.pa.WhitePieces[i].white)
                        {
                            c.pa.WhitePieces[i].x = c.pa.WhitePieces[i].dx;
                            c.pa.WhitePieces[i].y = c.pa.WhitePieces[i].dy;
                            c.Score.Black = c.Score.Black + c.pa.WhitePieces[i].value;
                        }
                        else
                        {
                            c.piececlickedloop.piece.x = c.piececlickedloop.piece.px;
                            c.piececlickedloop.piece.y = c.piececlickedloop.piece.py;
                            if (c.pieceset.WhiteTurn == true)
                            {
                                c.pieceset.WhiteTurn = false;
                            }
                            else
                            {
                                c.pieceset.WhiteTurn = true;
                            }
                        }
                    }
                    if (c.piececlickedloop.piece.x == c.pa.BlackPieces[i].x && c.piececlickedloop.piece.y == c.pa.BlackPieces[i].y && c.piececlickedloop.piece != c.pa.BlackPieces[i])
                    {
                        if (c.piececlickedloop.piece.white != c.pa.BlackPieces[i].white)
                        {
                            c.pa.BlackPieces[i].x = c.pa.BlackPieces[i].dx;
                            c.pa.BlackPieces[i].y = c.pa.BlackPieces[i].dy;
                        }
                        else
                        {
                            c.piececlickedloop.piece.x = c.piececlickedloop.piece.px;
                            c.piececlickedloop.piece.y = c.piececlickedloop.piece.py;
                            if (c.pieceset.WhiteTurn == true)
                            {
                                c.pieceset.WhiteTurn = false;
                            }
                            else
                            {
                                c.pieceset.WhiteTurn = true;
                            }
                        }
                    }
                }
                c.piececlickedloop.piece = null;
            }
        }

        // Sets all the pieces to their starting position, color and sprite
        public static void PiecesStart(Control c)
        {
            c.pa.WhitePawns = new Pawn[] { c.pieceset.wp1, c.pieceset.wp2, c.pieceset.wp3, c.pieceset.wp4, c.pieceset.wp5, c.pieceset.wp6, c.pieceset.wp7, c.pieceset.wp8 };
            c.pa.BlackPawns = new Pawn[] { c.pieceset.bp1, c.pieceset.bp2, c.pieceset.bp3, c.pieceset.bp4, c.pieceset.bp5, c.pieceset.bp6, c.pieceset.bp7, c.pieceset.bp8 };
            c.pa.WhitePieces = new Piece[] { c.pieceset.wr1, c.pieceset.wn1, c.pieceset.wb1, c.pieceset.wq, c.pieceset.wk, c.pieceset.wb2, c.pieceset.wn2, c.pieceset.wr2 };
            c.pa.BlackPieces = new Piece[] { c.pieceset.br1, c.pieceset.bn1, c.pieceset.bb1, c.pieceset.bq, c.pieceset.bk, c.pieceset.bb2, c.pieceset.bn2, c.pieceset.br2 };
            int[] PieceValue = { 5, 3, 3, 9, 0, 3, 3, 5 };
            int[] PieceDeadX = { 733, 733, 613, 693, 0, 653, 773, 773 };
            int[] BlackPieceDeadY = { 380, 456, 380, 380, 0, 380, 456, 380 };
            int[] WhitePieceDeadY = { 152, 76, 152, 152, 0, 152, 76, 152 };
            Image[] WhiteSprites = { Bitmap.FromFile("Images/WR.png"), Bitmap.FromFile("Images/WN.png"), Bitmap.FromFile("Images/WB.png"), Bitmap.FromFile("Images/WQ.png"), Bitmap.FromFile("Images/WK.png"), Bitmap.FromFile("Images/WB.png"), Bitmap.FromFile("Images/WN.png"), Bitmap.FromFile("Images/WR.png") };
            Image[] BlackSprites = { Bitmap.FromFile("Images/BR.png"), Bitmap.FromFile("Images/BN.png"), Bitmap.FromFile("Images/BB.png"), Bitmap.FromFile("Images/BQ.png"), Bitmap.FromFile("Images/BK.png"), Bitmap.FromFile("Images/BB.png"), Bitmap.FromFile("Images/BN.png"), Bitmap.FromFile("Images/BR.png") };
            c.pieceset.WhiteTurn = true;
            // Assign all the pieces their starting position (x, y), its color, and its Sprite.
            // white c.pawns
            for (var i = 0; i < 8; i++)
            {
                // White Pawns
                c.pa.WhitePawns[i].x = i * 76;
                c.pa.WhitePawns[i].px = c.pa.WhitePawns[i].x;
                c.pa.WhitePawns[i].y = 6 * 76;
                c.pa.WhitePawns[i].py = c.pa.WhitePawns[i].y;
                c.pa.WhitePawns[i].white = true;
                c.pa.WhitePawns[i].Sprite = Bitmap.FromFile("Images/WP.png");
                c.pa.WhitePawns[i].value = 1;

                // Black Pawns
                c.pa.BlackPawns[i].x = i * 76;
                c.pa.BlackPawns[i].px = c.pa.BlackPawns[i].x;
                c.pa.BlackPawns[i].y = 1 * 76;
                c.pa.BlackPawns[i].py = c.pa.BlackPawns[i].y;
                c.pa.BlackPawns[i].white = false;
                c.pa.BlackPawns[i].Sprite = Bitmap.FromFile("Images/BP.png");
                c.pa.BlackPawns[i].value = 1;

                // White Pieces
                c.pa.WhitePieces[i].x = i * 76;
                c.pa.WhitePieces[i].px = c.pa.WhitePieces[i].x;
                c.pa.WhitePieces[i].y = 7 * 76;
                c.pa.WhitePieces[i].py = c.pa.WhitePieces[i].y;
                c.pa.WhitePieces[i].white = true;
                c.pa.WhitePieces[i].Sprite = WhiteSprites[i];
                c.pa.WhitePieces[i].value = PieceValue[i];

                // Black Pieces
                c.pa.BlackPieces[i].x = i * 76;
                c.pa.BlackPieces[i].px = c.pa.BlackPieces[i].x;
                c.pa.BlackPieces[i].y = 0 * 76;
                c.pa.BlackPieces[i].py = c.pa.BlackPieces[i].y;
                c.pa.BlackPieces[i].white = false;
                c.pa.BlackPieces[i].Sprite = BlackSprites[i];
                c.pa.BlackPieces[i].value = PieceValue[i];

                // Set Position for Dead Piece
                if (i < 5)
                {
                    c.pa.WhitePawns[i].dx = 613 + i * 40;
                    c.pa.WhitePawns[i].dy = 0;

                    c.pa.BlackPawns[i].dx = 613 + i * 40;
                    c.pa.BlackPawns[i].dy = 532;
                }
                else
                {
                    c.pa.WhitePawns[i].dx = 613 + (i - 5) * 40;
                    c.pa.WhitePawns[i].dy = 76;

                    c.pa.BlackPawns[i].dx = 613 + (i - 5) * 40;
                    c.pa.BlackPawns[i].dy = 456;
                }
                c.pa.WhitePieces[i].dx = PieceDeadX[i];
                c.pa.WhitePieces[i].dy = WhitePieceDeadY[i];
                c.pa.BlackPieces[i].dx = PieceDeadX[i];
                c.pa.BlackPieces[i].dy = BlackPieceDeadY[i];
            }

            // white pieces
            for (var i = 0; i < 8; i++)
            {
            }

            //black pieces
            for (var i = 0; i < 8; i++)
            {
            }
        }
    }
}
